﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

using ManageMoney.BussinessLogic;
using ManageMoney.DataType;
using ManageMoney.Parameters.Recursos;
using ManageMoney.Web.AppCode;

namespace ManageMoney.Web.AppCode
{
    public class LoginView
    {
        public string LoginRedirect { get; set; }
        public string TituloSistema { get; set; }
        public string MensajeValidacion { get; set; }
    }

    public static class LoginIU
    {
        public static LoginView Login(string usuario, string contrasena)
        {
            LoginView vista = new LoginView();

            UsuarioLogic logic = new UsuarioLogic();

            UsuarioType u = logic.Login(usuario, contrasena);

            if (u == null)
            {
                vista.MensajeValidacion = MensajesUsuario.LoginInvalido;
                vista.TituloSistema = MensajesUsuario.TituloSistema;

                return vista;
            }

            Sesion.UsuarioMoney = u;
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(Convert.ToInt32(Sesion.UsuarioMoney.Id), Sesion.UsuarioMoney.Nombre, DateTime.Now, DateTime.Now.AddMinutes(900), false, "");

            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            HttpContext.Current.Response.Cookies.Add(authCookie);

            vista.MensajeValidacion = "";
            vista.LoginRedirect = FormsAuthentication.GetRedirectUrl(Sesion.UsuarioMoney.Email, false);

            return vista;
        }
    }
}